(function( $ ){
    var defaults = {};
    
    /* Tab micro-applications */
    var tabs = {
        
        main: {
            icon: "/vendor/socialmediaicons_v190/home.png",
            init: function(ntm, options) {
                    var content = new EJS({url: "/templates/main.ejs"}).render(options);
                    var tab = $("<div class='ntm-tab-content ntm-tab-main'></div>").html(content).css("display", "none");
                    $(".ntm-content").append(tab);
            },
            show: function(ntm, options) {},
            hide: function(ntm, options) {}
        },
		
        twitter: {
            icon: "/vendor/socialmediaicons_v190/twitter-16x16.png",
            init: function(ntm, options) {
                var tab = $("<div class='ntm-tab-content ntm-tab-twitter'></div>");
                $(".ntm-content").append(tab);
                console.log(options);
                $(".ntm-tab-twitter", ntm).liveTwitter(
                    options.query,
                    {
                        rate: 10000
                    }
                );
            },
            show: function() {},
            hide: function() {}
        },
        
	googlemaps: {
            icon: '/vendor/socialmediaicons_v190/map.png',
            init: function(ntm, options) {},
            show: function(ntm) {
                $(ntm).css('background-image', "url(/images/map.png)");
            },
            hide: function(ntm) {
                $(ntm).css('background-image', "url(/images/acme-corporation-background.jpg)");
            }
        },
        
        facebook: {
            icon: '/vendor/socialmediaicons_v190/facebook-16x16.png',
            init: function() {},
            show: function(ntm) {},
            hide: function(ntm) {}
        },
        
        linkedin: {
            icon: '/vendor/socialmediaicons_v190/linkedin-16x16.png',
            init: function() {},
            show: function(ntm) {},
            hide: function(ntm) {}
        }
    }

    var methods = {
        init : function( options ) {

            return this.each(function(){
                console.info("Init");
                
                var $this = $(this);
                $(this).html('');
                var data = $this.data('notimempu');
                
                // If the plugin hasn't been initialized yet
                if(!data) {
                    var defaults = {
                        tabs: {}
                    };
                    
                    options = $.extend(defaults, options);
                    console.info("Defaults + config:");
                    console.log(options);
                    
                    $this.data('notimempu', options);
                    
                    // Top-level layout
                    $this.append("<div class='ntm-container'><div class='ntm-content'></div><div class='ntm-tabs'></div></div>");
                    $this.css("background-image", "url(" + options.background + ")");
                    $this.css("background-repeat", "no-repeat");
                    $this.css("background-position", "0px 0px");

                    // Tabs
                    $.each(options.tabs, function(key, value) {
                        console.info("Creating tab buttons");
                        var image = $("<img />").attr("src", tabs[key].icon);
                        var button = $("<a href='#'></a>").append(image).click(function(event) {
                            event.preventDefault();
                            $this.notimempu("switchToTab", key);
                        });
                        var tab = $("<div></div>").addClass("ntm-tab").append(button);
                        $(".ntm-container .ntm-tabs", $this).append(tab);

                        console.info("Creating " + key + " tab");
                        tabs[key].init($this, options.tabs[key]);

                    });

                    $this.notimempu("switchToTab", "main");
                }
            });
        },
        
        destroy : function( ) {

            return this.each(function(){

                var $this = $(this),
                data = $this.data('notimempu');

                // Namespacing FTW
                $(window).unbind('.notimempu');
                data.notimempu.remove();
                $this.removeData('notimempu');

            })

        },
        
        switchToTab: function(tab) {
            var self = this;
            
            $.each(tabs, function(key, value) {
                tabs[key].hide(self);
            });
            
            $(".ntm-tab-content", this).hide();
            $(".ntm-tab-" + tab, this).show();
            
            tabs[tab].show(self);
        }
	
        // reposition : function( ) {},
        // show : function( ) {},
        // hide : function( ) {},
        // update : function( content ) {}
    };

    $.fn.notimempu = function( method ) {
    
        if ( methods[method] ) {
            return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
            return methods.init.apply( this, arguments );
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.notimempu' );
        }    
  
    };

})( jQuery );